<?php

namespace app\controller\v1;

use app\model\ApprovalModel;
use app\model\UserDepartmentModel;
use app\model\UserDepartmentRelationModel;
use app\validate\ApprovalValidate;

class Approval extends System
{
    public function list()
    {
        $approvalModel = new ApprovalModel();
        $res = $approvalModel->list($this->params);
        return $this->successJson($res);
    }

    /**
     * Notes: 审批节点类型
     * User: chenLei
     * Date: 9/7/25
     * @return \think\response\Json
     */
    public function getType()
    {
        $data = ApprovalModel::TYPE_LIST;
        return $this->successJson($data);
    }

    /**
     * Notes: 获取所有部门列表
     * User: chenLei
     * Date: 9/7/25
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function departmentList()
    {
        $res = (new UserDepartmentModel())->list();
        return $this->successJson($res);
    }

    /**
     * Notes: 根据部门获取下面的员工
     * User: chenLei
     * Date: 9/7/25
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDepartmentUserList()
    {
        $validate = new ApprovalValidate();

        if (!$validate->scene('getDepartmentUserList')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $userDepartmentRelationModel = new UserDepartmentRelationModel();
        $res = $userDepartmentRelationModel->getDepartmentUserList($this->params);

        return $this->successJson($res);

    }

    /**
     * Notes: 创建审核节点
     * User: chenLei
     * Date: 9/7/25
     * @return \think\response\Json
     */
    public function create()
    {
        $validate = new ApprovalValidate();
        if (!$validate->scene('create')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $approvalModel = new ApprovalModel();
        $res = $approvalModel->creates($this->params);

        if ($res === false) {
            return $this->errorJson($approvalModel->getError());
        }

        return $this->successJson();
    }


    /**
     * Notes:  编辑审批节点
     * User: chenLei
     * Date: 9/7/25
     * @return \think\response\Json
     */
    public function edit()
    {
        $validate = new ApprovalValidate();
        if (!$validate->scene('edit')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $approvalModel = new ApprovalModel();
        $res = $approvalModel->edit($this->params);

        if ($res === false) {
            return $this->errorJson($approvalModel->getError());
        }

        return $this->successJson();
    }


    /**
     * Notes: 删除审批节点
     * User: chenLei
     * Date: 9/7/25
     * @return \think\response\Json
     */
    public function del()
    {
        $validate = new ApprovalValidate();
        if (!$validate->scene('del')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $approvalModel = new ApprovalModel();
        $res = $approvalModel->del($this->params);

        if ($res === false) {
            return $this->errorJson($approvalModel->getError());
        }

        return $this->successJson();
    }
}